﻿using System;
using System.Collections.Generic;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using System.Data.Entity;
using Legazpi.Payroll.DataAccess.Repositories;
using Legazpi.Payroll.DataAccess.Base;

namespace Legazpi.Payroll.BusinessLogic.Services
{
    public class PharaphernaliaService : IPharaphernaliaService
    {
       #region Fields

        private DbContext _dbContext;
        private IPharaphernaliaRepository _pharaphernaliaRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public PharaphernaliaService()
            : this(new DatabaseFactory())
        {

        }

        public PharaphernaliaService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _pharaphernaliaRepository = new PharaphernaliaRepository(_dbContext);
            }
        }

        #endregion

        public Pharaphernalia GetById(int id)
        {
            return _pharaphernaliaRepository.GetById(id);
        }

        public IEnumerable<Pharaphernalia> GetAll()
        {
            return _pharaphernaliaRepository.GetAll();
        }

        public IEnumerable<Pharaphernalia> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(Pharaphernalia pharaphernalia)
        {
            _pharaphernaliaRepository.Add(pharaphernalia);
        }

        public void Update(Pharaphernalia pharaphernalia)
        {
            _pharaphernaliaRepository.Update(pharaphernalia);
        }

        public void Delete(int id)
        {
            var pharaphernalia = GetById(id);
            if (pharaphernalia != null)
                _pharaphernaliaRepository.Delete(pharaphernalia);
        }

        public bool Save()
        {
            string message;
            _pharaphernaliaRepository.Save(out message);
            return message == "Saved";
        }
    }
}
